<template>
  <div class="login-page">
    <nav-bar></nav-bar>
    <div class="login-content">
      <div class="login-container">
        <div class="login-left">
          <img src="@/assets/register-illustration.png" alt="登录插图" class="large-illustration">
        </div>
        <div class="login-right">
          <h2>欢迎回到学了喵</h2>
          <p class="login-desc">请登录您的账号继续学习</p>
          
          <div class="custom-input-container">
            <input 
              v-model="account"
              class="custom-input" 
              placeholder="请输入手机号/邮箱" 
              maxlength="30"
            />
          </div>
          
          <div class="custom-input-container">
            <input 
              v-model="password"
              class="custom-input" 
              type="password"
              placeholder="请输入密码" 
            />
          </div>
          
          <div class="remember-forgot">
            <label class="remember">
              <input type="checkbox" v-model="rememberMe">
              <span>记住我</span>
            </label>
            <a href="#" class="forgot">忘记密码?</a>
          </div>
          
          <button class="login-btn" @click="handleLogin">登录</button>
          
          <div class="register-link">
            还没有账号? <router-link to="/register">立即注册</router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import NavBar from '@/components/NavBar.vue'
import axios from 'axios'

export default {
  name: 'LoginView',
  components: {
    NavBar
  },
  data() {
    return {
      account: '',
      password: '',
      rememberMe: false
    }
  },
  methods: {
    async handleLogin() {
      if (!this.account || !this.password) {
        alert('请输入用户名和密码');
        return;
      }
      try {
        const response = await axios.post('/api/auth/login', {
          username: this.account,
          password: this.password
        });
        if (response.data.code === 200) {
          alert('登录成功');
          // 打印 userId
          console.log('登录接口返回的 userId:', response.data.userId);
          // 存储token到localStorage
          if (response.data.token) {
            localStorage.setItem('token', response.data.token);
          }
          // 存储用户名到localStorage
          if (response.data.username) {
            localStorage.setItem('username', response.data.username);
          } else {
            localStorage.setItem('username', this.account);
          }
          // 存储userId到localStorage
          if (response.data.userId) {
            localStorage.setItem('userId', response.data.userId);
          }
          this.$router.push('/'); // 跳转到主页
        } else {
          alert(response.data.message || '登录失败');
        }
      } catch (error) {
        alert(error.response?.data?.message || '登录失败');
      }
    }
  }
}
</script>

<style scoped>
.login-page {
  width: 100%;
  min-height: 100vh;
  background-color: #fff;
}

.login-content {
  padding-top: 80px;
}

.login-container {
  width: 1000px;
  margin: 0 auto;
  display: flex;
  gap: 60px;
  align-items: center;
}

.login-left {
  flex: 1;
}

.login-left img {
  width: 100%;
  max-width: 500px;
}

.large-illustration {
  width: 120%;
  max-width: 600px;
  margin-left: -40px;
}

.login-right {
  flex: 1;
  padding: 40px;
  max-width: 400px;
}

h2 {
  font-size: 32px;
  color: #333;
  margin-bottom: 15px;
  font-weight: 500;
}

.login-desc {
  color: #666;
  font-size: 14px;
  margin-bottom: 30px;
}

.custom-input-container {
  margin: 20px 0;
  width: 100%;
}

.custom-input {
  width: 100%;
  height: 45px;
  border: 1px solid #333;
  border-radius: 0;
  padding: 0 15px;
  font-size: 14px;
  box-sizing: border-box;
  outline: none;
}

.custom-input:focus {
  border-color: #6d28d2;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 20px 0;
}

.remember {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #666;
  font-size: 14px;
}

.forgot {
  color: #6d28d2;
  text-decoration: none;
  font-size: 14px;
}

.login-btn {
  width: 100%;
  height: 45px;
  background: #6d28d2;
  color: white;
  border: none;
  font-size: 16px;
  cursor: pointer;
  margin-top: 10px;
}

.login-btn:hover {
  opacity: 0.9;
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
  font-size: 14px;
}

.register-link a {
  color: #6d28d2;
  text-decoration: none;
}
</style>